package com.ly.mpayment.rest.system.ele;

import java.math.BigDecimal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ly.mpayment.core.util.BigUtil;
import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.persist.entity.ele.RechargeTask;
import com.ly.mpayment.persist.service.task.IRechargeTaskService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;

import cn.jpush.api.utils.StringUtils;
import net.sf.json.JSONObject;

@Component
@Path("system/task/")
public class RechargeTaskRest extends AbsRest {

	@Autowired
	IRechargeTaskService taskService;

	@Autowired
	IUserService userService;

	@SuppressWarnings("static-access")
	@Path("findPageTask")
	@POST
	public String findPageTask(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService,"taskCity","start","pageSize","state");
		if(result == null){
			Map<String, Object> parameters = MapUtil.getMap();
			JSONObject json = JSONObject.fromObject(data);
			if(!StringUtils.isEmpty(json.getString("taskCity"))){
				parameters.put(":taskCity", json.getString("taskCity"));
			}
			if(!StringUtils.isEmpty(json.getString("state"))){
				parameters.put(":state", json.getInt("state"));
			}
			return ResultMessage.getSucceed(JsonUtils.excludes(this.taskService.findPageTask(parameters, json.getInt("start"), json.getInt("pageSize")), new String[]{""}));
		}
		return result;
	}

	@SuppressWarnings("static-access")
	@Path("updateTask")
	@POST
	public String updateTask(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,request, userService, "taskId","taskCity","taskAmount");
		if(result == null){
			JSONObject json = JSONObject.fromObject(data);
			RechargeTask task = this.taskService.findTask(json.getString("taskId"));
			if(task != null){
				task.setTaskAmount(BigUtil.getBigDecimal(json.getString("taskAmount")));
				task.setTaskCity(json.getString("taskCity"));
				if(taskService.updateTask(task)){
					return ResultMessage.getSucceed(null);
				}
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}

	@SuppressWarnings("static-access")
	@Path("saveTask")
	@POST
	public String saveTask(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,request, userService, "taskCity","taskAmount");
		if(result == null){
			JSONObject json = JSONObject.fromObject(data);
			RechargeTask task = new RechargeTask();
			task.setTaskCity(json.getString("taskCity"));
			task.setProvince(json.getString("province"));
			task.setRechargeAmount(BigDecimal.ZERO);
			task.setTaskAmount(BigUtil.getBigDecimal(json.getString("taskAmount")));
			if(taskService.saveTask(task)){
				return ResultMessage.getSucceed(null);
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}

	@SuppressWarnings("static-access")
	@Path("updateTaskState")
	@POST
	public String updateTaskState(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,request, userService, "taskId","state");
		if(result == null){
			JSONObject json = JSONObject.fromObject(data);
			//检查当前是否已经在使用的数据
//			RechargeTask taskCurrent = taskService.findTask(json.getString("taskId"));
//			if(taskCurrent != null && json.getInt("state") == 1){
//				return ResultMessage.getFailt(null,"请先关闭正在使用中的城市");
//			}
			RechargeTask task = this.taskService.findTask(json.getString("taskId"));
			if(task != null){
				task.setState(json.getInt("state"));
				if(json.getInt("state") == 1){
					task.setRechargeAmount(BigDecimal.ZERO);
				}
				if(taskService.updateTask(task)){
					return ResultMessage.getSucceed(null);
				}
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}
}
